1   /*
2    * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /**
25   * @test
26   * @bug 6476665
27   * @summary Verifies color conversion of Direct Color Model based images
28   * @run main ColConvDCMTest
29   */
30  
31  import java.awt.color.ColorSpace;
32  import java.awt.image.BufferedImage;
33  import java.awt.image.ColorConvertOp;
34  import java.io.File;
35  import java.io.IOException;
36  import javax.imageio.ImageIO;
37  
38  public class ColConvDCMTest extends ColConvTest {
39  
40      /*
41       * Test case descriptors: <imgType> <rBits> <gBits> <bBits> <csNum> <gldNum>
42       */
43      final static int [][] imgTypes = {
44          {BufferedImage.TYPE_INT_ARGB, 8, 8, 8, 0, 0},
45          {BufferedImage.TYPE_INT_ARGB, 8, 8, 8, 1, 3},
46  
47          {BufferedImage.TYPE_INT_RGB, 8, 8, 8, 0, 0},
48          {BufferedImage.TYPE_INT_RGB, 8, 8, 8, 1, 3},
49  
50          {BufferedImage.TYPE_INT_BGR, 8, 8, 8, 0, 0},
51          {BufferedImage.TYPE_INT_BGR, 8, 8, 8, 1, 3},
52  
53          {BufferedImage.TYPE_USHORT_555_RGB, 5, 5, 5, 0, 1},
54          {BufferedImage.TYPE_USHORT_555_RGB, 5, 5, 5, 1, 4},
55  
56          {BufferedImage.TYPE_USHORT_565_RGB, 5, 6, 5, 0, 2},
57          {BufferedImage.TYPE_USHORT_565_RGB, 5, 6, 5, 1, 5}
58      };
59  
60      final static int [] cSpaces = {
61          ColorSpace.CS_sRGB,
62          ColorSpace.CS_LINEAR_RGB,
63      };
64  
65      final static double ACCURACY = 2.5;
66  
67      final static String [] gldImgNames = {
68          "SRGB.png", "SRGB555.png", "SRGB565.png", "LRGB.png", "LRGB555.png",
69          "LRGB565.png"
70      };
71  
72      static BufferedImage [] gldImages = null;
73  
74      static boolean testImage(int type, int rBits, int gBits, int bBits,
75                                int cs, BufferedImage gldImage,
76                                double accuracy)
77      {
78          BufferedImage src = ImageFactory.createDCMImage(type, cs);
79          BufferedImage dst = ImageFactory.createDstImage(
80              BufferedImage.TYPE_INT_RGB);
81          ColorConvertOp op = new ColorConvertOp(null);
82  
83          op.filter(src, dst);
84  
85          ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
86                                                    bBits);
87          boolean result = cmp.compare(gldImage, dst);
88          if (!result) {
89              System.err.println(cmp.getStat());
90          }
91          return result;
92      }
93  
94       static boolean testSubImage(int x0, int y0, int dx, int dy, int type,
95                                   int rBits, int gBits, int bBits,
96                                   int cs, BufferedImage gldImage,
97                                   double accuracy)
98       {
99          BufferedImage src = ImageFactory.createDCMImage(type, cs);
100         BufferedImage subSrc = src.getSubimage(x0, y0, dx, dy);
101         BufferedImage dst = ImageFactory.createDstImage(
102             BufferedImage.TYPE_INT_RGB);
103         BufferedImage subDst = dst.getSubimage(x0, y0, dx, dy);
104 
105         ColorConvertOp op = new ColorConvertOp(null);
106 
107         op.filter(subSrc, subDst);
108 
109         ImageComparator cmp = new ImageComparator(accuracy, rBits, gBits,
110                                                   bBits);
111         boolean result = cmp.compare(subDst, gldImage, x0, y0, dx, dy);
112         if (!result) {
113             System.err.println(cmp.getStat());
114         }
115         return result;
116      }
117 
118      synchronized public static void initGoldenImages() {
119         if (gldImages == null) {
120             gldImages = new BufferedImage[gldImgNames.length];
121             for (int i = 0; i < gldImgNames.length; i++) {
122                 try {
123                     File gldFile = new File(System.getProperty("test.src", "."),
124                                             gldImgNames[i]);
125 
126                     gldImages[i] = ImageIO.read(gldFile);
127                 } catch (IOException e) {
128                     throw new RuntimeException("Cannot initialize golden " +
129                                                "image: " + gldImgNames[i]);
130                 }
131             }
132         }
133      }
134 
135      public void init() {
136         initGoldenImages();
137      }
138 
139      public void runTest() {
140         for (int i = 0; i < imgTypes.length; i++) {
141             BufferedImage gldImage = gldImages[imgTypes[i][5]];
142 
143             if (!testImage(imgTypes[i][0], imgTypes[i][1], imgTypes[i][2],
144                            imgTypes[i][3], cSpaces[imgTypes[i][4]], gldImage,
145                            ACCURACY))
146             {
147                 throw new RuntimeException(
148                     "Invalid result of the ColorConvertOp for " +
149                     "ColorSpace:" + getCSName(cSpaces[imgTypes[i][4]]) +
150                     " Image type:" +
151                     getImageTypeName(imgTypes[i][0]) + ". Golden image:" +
152                     gldImgNames[imgTypes[i][5]]);
153             }
154 
155             if (!testSubImage(SI_X, SI_Y, SI_W, SI_H, imgTypes[i][0],
156                               imgTypes[i][1], imgTypes[i][2], imgTypes[i][3],
157                               cSpaces[imgTypes[i][4]], gldImage, ACCURACY))
158             {
159                 throw new RuntimeException(
160                     "Invalid result of the ColorConvertOp for " +
161                      "ColorSpace:" + getCSName(cSpaces[imgTypes[i][4]]) +
162                      " Image type:" +
163                      getImageTypeName(imgTypes[i][0]) + ". Golden image:" +
164                      gldImgNames[imgTypes[i][5]]);
165             }
166         }
167      }
168 
169      public static void main(String [] args) throws Exception {
170          ColConvDCMTest test = new ColConvDCMTest();
171          test.init();
172          test.run();
173      }
174 }